package com.matrix.index.ui.anime

import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.navigation.NavController
import androidx.navigation.findNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.matrix.basecore.base.BaseActivity
import com.matrix.basecore.constant.Constant
import com.matrix.index.R
import com.matrix.index.databinding.ActivityWallpaperAnimBinding

@Route(path = Constant.ARouterPath.AnimWallpaperActivityPath)
class AnimWallpaperActivity : BaseActivity<ActivityWallpaperAnimBinding>() {

    private lateinit var navController: NavController

    override fun getViewBinding(): ActivityWallpaperAnimBinding {
        enableEdgeToEdge()
        return ActivityWallpaperAnimBinding.inflate(layoutInflater)
    }

    override fun initView() {
        mViewBinding.main.post {
            navController = findNavController(R.id.anim_fragment)
            var isChange = SPUtils.getInstance().getBoolean(Constant.Value.SP_WALLPAPER, false)
            if (isChange) {
                navController.navigate(R.id.galleryFragment)
            } else {
                navController.navigate(R.id.animFragment)
            }
        }
    }

    override fun initListener() {
        mViewBinding.ivChange.setOnClickListener {
            showDialog()
        }
        mViewBinding.ivBack.setOnClickListener { finish() }
    }

    private fun showDialog() {
        var build = AlertDialog.Builder(this)
        var dialog = build.setTitle("切换首页壁纸")
            .setMessage("是否切换当前页作为首页的壁纸呈现？")
            .setNegativeButton("取消") { _, _ ->
            }.setPositiveButton("确认") { _, _ ->
                var isChange = SPUtils.getInstance().getBoolean(Constant.Value.SP_WALLPAPER, false)
                if (isChange) {
                    SPUtils.getInstance().put(Constant.Value.SP_WALLPAPER, false)
                } else {
                    SPUtils.getInstance().put(Constant.Value.SP_WALLPAPER, true)
                }
                finish()
            }
            .create()
        dialog.show()
    }
}